package com.quasar.hpatient.module.comm_camera;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.widget.Toast;

import com.quasar.hpatient.R;

import java.io.File;

import javax.xml.transform.ErrorListener;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import lib.quasar.base.frame.BaseActivity;
import lib.quasar.camera.CameraView;
import lib.quasar.camera.listener.OnCameraChangeListener;
import lib.quasar.compress.CompressManager;
import lib.quasar.compress.OnCompressChangeListener;
import lib.quasar.context.BaseConstant;
import lib.quasar.util.CardUtil;
import lib.quasar.util.FileUtil;
import lib.quasar.util.LogUtil;
import lib.quasar.util.ToastUtil;

public class CameraActivity extends BaseActivity {

    public static final String PATH = "PATH";
    public static final String WIDTH = "WIDTH";
    public static final String HEIGHT = "HEIGHT";
    public static final String SIZE = "SIZESIZE";
    public static final int RESULT_CODE = 2002;

    @Override
    public int initView() {
        return R.layout.activity_comm_camera;
    }

    @Override
    public void initDataLocal() {

        final CameraView camera = findViewById(R.id.activity_camera);
        //设置视频保存路径
        camera.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");
        //设置只能录像或只能拍照或两种都可以（默认两种都可以）
        camera.setFeatures(CameraView.BUTTON_STATE_BOTH);
        camera.setTip("轻触屏幕, 相机对焦");
        camera.setMediaQuality(CameraView.MEDIA_QUALITY_MIDDLE);
//        camera.setErrorLisenter(new ErrorListener() {
//            @Override
//            public void onError() {
//            }
//
//            @Override
//            public void AudioPermissionError() {
//                Toast.makeText(getApplicationContext(), "给点录音权限可以?", Toast.LENGTH_SHORT).show();
//            }
//        });

        camera.setOnCameraChangeListener(new OnCameraChangeListener() {

            @SuppressLint("CheckResult")
            @Override
            public void onPhotoSucc(Bitmap bitmap) {
                LogUtil.e("uuu", "onPhotoSucc");

                Observable.create((ObservableOnSubscribe<String>) e -> {

                    final String floderPath = CardUtil.getSDCardPath() + BaseConstant.CACHE_CAMERA;
                    final String name = System.currentTimeMillis() + ".jpg";

                    // 压缩图片
                    CompressManager.syncCompress(true, 85, floderPath, name, bitmap, new OnCompressChangeListener() {
                        @Override
                        public void onCompressStart() {
                        }

                        @Override
                        public void onCompressFinish(String filePath) {
                            LogUtil.e("", "onCompressFinish");
                            e.onNext(filePath);
                        }

                        @Override
                        public void onCompressError(int errorNum, String description) {
                            LogUtil.e("", "onCompressError");
                        }
                    });
                }).subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(path -> {
                                    Intent intent = new Intent();
                                    intent.putExtra(PATH, path);

                                    final File file = new File(path);
                                    intent.putExtra(SIZE, FileUtil.getFileSize(file));

                                    if (null != bitmap) {
                                        intent.putExtra(WIDTH, bitmap.getWidth());
                                        intent.putExtra(HEIGHT, bitmap.getHeight());
                                        bitmap.recycle();
                                        LogUtil.e("", "释放bitmap");
                                    }
                                    setResult(RESULT_CODE, intent);
                                    onBackPressed();
                                    ToastUtil.showToast("分线程: 压缩图片结束");
                                }, throwable -> {
                                    ToastUtil.showToast("分线程: 压缩图片失败");
                                },
                                () -> {
                                    ToastUtil.showToast("分线程: 压缩图片结束");
                                },
                                disposable -> {
                                    ToastUtil.showToast("分线程: 开始压缩图片");
                                });
            }

            @Override
            public void onRecordSucc(String url, Bitmap firstFrame) {
            }
        });
    }

    @Override
    protected void onResume() {

        final CameraView camera = findViewById(R.id.activity_camera);
        camera.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {

        final CameraView camera = findViewById(R.id.activity_camera);
        camera.onPause();
        super.onPause();
    }
}
